//app.js1
const {
  $Message
} = require('/component/iView/base/index');

App({
  globalData: {
    baseAPI: "https://exam.hq0311.com/",
    //baseAPI: "https://exam.98159.net1/",
    //baseAPI: "https://d.hbzi.cn/1",
    //baseAPI: "http://localhost:8086/wx",
    pageSize: 100,
  },
  onLaunch: function () {
    let _this = this
    let token = wx.getStorageSync('token')
    //console.log("token11111==",token)1
    if (null == token || token == '') {
      const accountInfo = wx.getAccountInfoSync()
      wx.login({
        success(wxres) {
          //console.log(wxres)
          if (wxres.code) {
            _this.formPost('/wx/user/checkBind', {
              "code": wxres.code,
              "appid": accountInfo.miniProgram.appId,
            }).then(res => {
              //console.log(res)
              if(res.code == 200){
                wx.setStorageSync('token', res.token)
                //console.log("res.token===",res.token)
                wx.setStorageSync('empinfo', res.empinfo)
                wx.setStorageSync('session', res.session)
                wx.reLaunch({
                  url: '/pages/tiku/fenlei',
                })
              }else if(res.code == 201){
                wx.setStorageSync('session', res.session)
                wx.reLaunch({
                    url: '/pages/user/bind/index',
                });
              }else{
                _this.message(res.msg, 'error')
              }
            }).catch(e => {
              _this.message(e, 'error')
            })
          }else {
            _this.message(wxres.errMsg, 'error')
          }
        }
      })
    }
  },
  message: function (content, type) {
    $Message({
      content: content,
      type: type
    });
  },
  formPost: function (url, data) {
    let _this = this
    return new Promise(function (resolve, reject) {
      wx.showNavigationBarLoading();
      wx.request({
        url: _this.globalData.baseAPI + url,
        header: {
          'content-type': 'application/json; charset=utf-8',
          'token': wx.getStorageSync('token')
        },
        method: 'POST',
        data,
        success(res) {
          //console.log("sssssss===",res)
          if (res.statusCode !== 200 || typeof res.data !== 'object') {
            reject('网络出错')
            return false;
          }

          // if (res.data.code === 400) {
          //   let token = res.data.response
          //   console.log("拦截token",token)
          //   wx.setStorageSync('token', token)
          //   wx.request({
          //     url: _this.globalData.baseAPI + url,
          //     header: {
          //       'content-type': 'application/json; charset=utf-8',
          //       'token': wx.getStorageSync('token')
          //     },
          //     method: 'POST',
          //     data,
          //     success(result) {
          //       resolve(result.data);
          //       return true;
          //     }
          //   })
          // } else 
          if (res.data.code === 401) {
            //wx.clearStorageSync()
            wx.reLaunch({
              url: '/pages/user/bind/index',
            });
            return false;
          } else if (res.data.code === 500) {
            reject(res.data.msg)
            return false;
          } else if (res.data.code === 501) {
            reject(res.data.msg)
            return false;
          } else {
            resolve(res.data);
            return true;
          }
        },
        fail(res) {
          reject(res.errMsg)
          return false;
        },
        complete(res) {
          wx.hideNavigationBarLoading();
        }
      })
    })
  },
  onShow:function(){
    let _this = this
    let empinfo = wx.getStorageSync('empinfo')
    //console.log("www",empinfo)
    if(null == empinfo || empinfo == ''){
      wx.reLaunch({
        url: '/pages/user/bind/index',
    })
    }else{
      //console.log("bbb")
       _this.formPost("/wx/user/checkIsFreeze",{"employeeId":empinfo.employeeId})
    }
  }
})